package com.hospital.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hospital.factory.DaoFactory;
import com.hospital.vo.Expert;

public class AddExp extends HttpServlet {

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String eid = request.getParameter("eid");
		String kid = request.getParameter("kid");
		String ename = request.getParameter("ename");
		String content = request.getParameter("content");
		Expert expert = new Expert();
		expert.setKid(Integer.parseInt(kid));
		
		expert.setEname(ename);
		expert.setContent(content);
		expert.setPubdate(new Date());
		int index = 0;
		String imgdir = null;
		if((index = content.indexOf("img src=\"")) != -1) {
			
			imgdir = content.substring(index+9);
			if((index = imgdir.indexOf("ueditor/jsp/")) != -1) {
				imgdir = imgdir.substring(index);
				imgdir = imgdir.substring(0, imgdir.indexOf("\""));
				expert.setImgdir(imgdir);
			}
		}
		boolean flag = false;
		if(eid == null || "".equals(eid)){
			try {
				flag = DaoFactory.getIExpertDaoInstance().doCreate(expert);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			expert.setEid(Integer.parseInt(eid));
			try {
				flag = DaoFactory.getIExpertDaoInstance().doUpdate(expert);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if(flag == true) {
			response.sendRedirect(request.getContextPath() + "/admin/explist");
		}
		
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}

}
